import { ElMessage } from 'element-plus'

export function useShortActions() {
  const userStore = useUserStore()
  const router = useRouter()

  /**
   * 获取短视频的用户交互状态
   */
  const getShortUserStatus = async (shortId: number) => {
    const status = {
      isLike: false,
      isCollection: false
    }

    if (!userStore.isLoggedIn) {
      return status
    }

    try {
      // 获取点赞状态
      const likeData = await $fetch(`/dev-api/short/member/like/status`, {
        headers: {
          'Authorization': `Bearer ${userStore.token}`
        },
        params: { shortId }
      })

      if (likeData.code === 200) {
        status.isLike = likeData.data || false
      }

      // 获取收藏状态
      const collectionData = await $fetch(`/dev-api/short/member/collection/status`, {
        headers: {
          'Authorization': `Bearer ${userStore.token}`
        },
        params: { shortId }
      })

      if (collectionData.code === 200) {
        status.isCollection = collectionData.data || false
      }
    } catch (error) {
      console.error('获取短视频用户状态失败:', error)
    }

    return status
  }

  /**
   * 处理点赞/取消点赞
   */
  const handleLike = async (
    event: Event,
    short: any,
    currentStatus: { isLike: boolean }
  ) => {
    if (!userStore.isLoggedIn) {
      router.push('/login')
      return
    }

    const target = event.target as HTMLElement

    try {
      if (currentStatus.isLike) {
        // 取消点赞
        const data = await $fetch('/dev-api/short/member/like/cancel', {
          method: 'POST',
          headers: {
            'Authorization': `Bearer ${userStore.token}`
          },
          body: {
            shortId: short.shortId
          }
        })

        if (data.code === 200) {
          currentStatus.isLike = false
          short.likes = Math.max(0, (short.likes || 0) - 1)
        }
      } else {
        // 点赞
        const data = await $fetch('/dev-api/short/member/like', {
          method: 'POST',
          headers: {
            'Authorization': `Bearer ${userStore.token}`
          },
          body: {
            shortId: short.shortId
          }
        })

        if (data.code === 200) {
          currentStatus.isLike = true
          short.likes = (short.likes || 0) + 1

          // 添加动画效果
          await nextTick()
          target.classList.add('animate-bounce')
          setTimeout(() => {
            target.classList.remove('animate-bounce')
          }, 1000)
        }
      }
    } catch (error: any) {
      console.error('点赞操作失败:', error)
      ElMessage.error(error.data?.msg || '操作失败')
    }
  }

  /**
   * 处理收藏/取消收藏
   */
  const handleCollection = async (
    event: Event,
    short: any,
    currentStatus: { isCollection: boolean }
  ) => {
    if (!userStore.isLoggedIn) {
      router.push('/login')
      return
    }

    const target = event.target as HTMLElement

    try {
      if (currentStatus.isCollection) {
        // 取消收藏
        const data = await $fetch('/dev-api/short/member/collection/cancel', {
          method: 'POST',
          headers: {
            'Authorization': `Bearer ${userStore.token}`
          },
          body: {
            shortId: short.shortId
          }
        })

        if (data.code === 200) {
          currentStatus.isCollection = false
          short.collection = Math.max(0, (short.collection || 0) - 1)
        }
      } else {
        // 收藏
        const data = await $fetch('/dev-api/short/member/collection', {
          method: 'POST',
          headers: {
            'Authorization': `Bearer ${userStore.token}`
          },
          body: {
            shortId: short.shortId
          }
        })

        if (data.code === 200) {
          currentStatus.isCollection = true
          short.collection = (short.collection || 0) + 1

          // 添加动画效果
          await nextTick()
          target.classList.add('animate-bounce')
          setTimeout(() => {
            target.classList.remove('animate-bounce')
          }, 1000)
        }
      }
    } catch (error: any) {
      console.error('收藏操作失败:', error)
      ElMessage.error(error.data?.msg || '操作失败')
    }
  }

  return {
    getShortUserStatus,
    handleLike,
    handleCollection
  }
}
